package org.example.controller;

/**
 * @author liyishan
 * @date 2024/1/10 14:51
 * @apiNote
 */

import lombok.extern.slf4j.Slf4j;
import org.example.annotation.AccessInterceptor;
import org.example.annotation.LeakyBucketLimit;
import org.example.annotation.SlideWindowLimit;
import org.example.annotation.TimeStatisticsLimit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试用例控制器
 * @author Mr.Tao
 */
@RestController
@Slf4j
public class TestController {

    @TimeStatisticsLimit(time = 5, count = 2)
    @GetMapping("/time")
    public String time() {
        log.info("time");
        return "time";
    }
    @LeakyBucketLimit(capacity = 3, leakRate = 2000)
    @GetMapping("/leaky")
    public String leaky() {
        log.info("time");
        return "leaky";
    }

    @SlideWindowLimit(time = 3, count = 1)
    @GetMapping("/slide")
    public String slide() {
        return "slide";
    }

    @AccessInterceptor(key = "fingerprint", fallbackMethod = "loginErr", permitsPerSecond = 1.0d, blacklistCount = 1)
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login(String fingerprint, String uId, String token) {
        log.info("模拟登录 fingerprint:{}", fingerprint);
        return "模拟登录：登录成功 " + uId;
    }

    public String loginErr(String fingerprint, String uId, String token) {
        return "频次限制，请勿恶意访问！";
    }

    @GetMapping("/normal")
    public String normal() {
        return "normal";
    }

}